<?php
/**
 * vodlist
 * 点播文件
 * @author Administrator
 * @version
 */
require_once ROOT_PATH . '/library/Soooner/Encode/encoder.php';
class Model_Tables_Vodlist extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'vodlist';
    /**
     * add
     * 增加点播文件
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'vid' => $params['vid'],
        'title' => $params['title'],
		'url' => $params['url'],   
        'urllow' => $params['urllow'],       
		'uuid' => $this->uuid($params['vid'],$params['url']),
        'uuidlow' => $this->uuid($params['vid'],$params['urllow']),
		'urltype' => $params['urltype'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑点播文件
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'vid' => $params['vid'],
        'title' => $params['title'],
		'url' => $params['url'],
        'urllow' => $params['urllow'],    
        'uuid' => $this->uuid($params['vid'],$params['url']),
        'uuidlow' => $this->uuid($params['vid'],$params['urllow']),
		'urltype' => $params['urltype'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑点播文件
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	if($field == "url"){
    		$info = $this->getInfo($id);
    		$data["uuid"] = $this->uuid($info["vid"],$value);
    	}
    	
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转点播文件
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update vodlist set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除点播文件
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取点播文件信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取点播文件数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "",$vid =-1)
    { 
    	$vid = intval($vid);
    	$db = $this->getAdapter();
    	$select = $this->select()->from($this->_name,'count(*) as num');
    	$where = $db->quoteInto(" vid=?", $vid);
        if($s!="")  
    	{
			$where.= " and ".$db->quoteInto(" title like ?", "%$s%");
    	}
    	$select = $select->where($where);
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取点播文件数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "",$vid =-1)
    {
    	$vid = intval($vid);
    	$db = $this->getAdapter();
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('orderid asc','id desc');
        $where = $db->quoteInto(" vid=?", $vid);
        if($s!="")     
		{
			$where.= " and ".$db->quoteInto(" title like ?", "%$s%");			
		}
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /**
     * getAllList
     * 获取点播文件的节目列表
     * @param $vid  节目编号
     * */
    public function getAllList($vid =-1){
    	$vid = intval($vid);
    	$db = $this->getAdapter();    	
        $where = $db->quoteInto(" vid=?", $vid);
        $result = $this->fetchAll($where)->toArray();
        return $result;
    }
    private function uuid($vid,$url){
    	$db = $this->getAdapter();
    	$url=$this->parseurl($url);
    	$bitrate = $db->fetchOne("select bitrate from vod where vid = :vid",array("vid"=>$vid));
    	//print_r($bitrate);exit;
    	$str = "[chn]|$url|A|[/chn][ac]||[/ac][pipe]|1|$bitrate|[/pipe][dev]||[/dev][user]|[/user][box][/box]";
    	$rl = new encoder();
    	return $rl->rl_encode($str,1);
    }
	public function parseurl($url="")
	{
		$url = urlencode($url);
		$a = array("%3A", "%2F", "%40");
		$b = array(":", "/", "@");
		$url = str_replace($a, $b, $url);
		return $url;
	}
 	public function uu($url){
        $str = "[chn]|$url|A|[/chn][ac]||[/ac][pipe]|1|4000|[/pipe][dev]||[/dev][user]|[/user][box][/box]";
        $rl = new encoder();
        return $rl->rl_encode($str,1);
    }
}
